package com.huiquan.slang.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.constants.AnalysisSentenceConstant;
import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.GetListUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.slang.dao.SlangRuleDao;
import com.huiquan.slang.domain.SlangRule;
import com.huiquan.slang.domain.SlangVocabulary;
import com.mysql.jdbc.StringUtils;

@Service
public class SlangRuleService extends BaseService {

	@Autowired
	private SlangRuleDao slangRuleDao;

	public ModelAndView index(Integer start) {
		Map<String, Object> param = new HashMap<>();

		int totalSize = slangRuleDao.retrieveSize();
		Map<String, Integer> pageNo = GetListUtil.getPageNoMap(totalSize, start);

		int endPage = pageNo.get("endPage");
		start = pageNo.get("start");
		int startIndex = pageNo.get("startIndex");

		List<SlangRule> list = new ArrayList<>();

		if (startIndex < totalSize) {
			param.put("startIndex", startIndex);
			list = slangRuleDao.retrieveList(param);
		}

		Map<String, Object> map = GetListUtil.getReturnMap(4, totalSize, start, startIndex, endPage, list, null);
		return new ModelAndView("slang/rule_index", map);
	}

	public ModelAndView rulePreEdit(Integer start, String id, String vocabulary, User user) {
		// 如果id不为空则表示是修改，否则为新增
		SlangRule slangRule = new SlangRule();

		if (id != null && !id.isEmpty()) {
			slangRule = slangRuleDao.retrieveObjectById(Long.parseLong(id));
			vocabulary = slangRule.getVocabulary();
		} else {
			slangRule.setVocabulary(vocabulary);
			slangRule.setRule(vocabulary + "_ad_O");
			slangRule.setModifierId(user.getUserId());
			slangRule.setModifierName(user.getRealName());
			slangRule.setId(slangRuleDao.insert(slangRule));
		}

		List<SlangVocabulary> vs = getVocabularys(slangRule.getRule());

		Map<String, String> characterMap = new LinkedHashMap<>();
		characterMap.putAll(AnalysisSentenceConstant.getPropertyMapByType(AnalysisSentenceConstant.CHARACTER_LABEL));

		for (SlangVocabulary v : vs) {
			if (!characterMap.containsKey(v.getCharacteristic())) {
				characterMap.put(v.getCharacteristic(), v.getCharacteristic());
			}
		}

		SlangVocabulary oldVocabulary = new SlangVocabulary();
		String[] temp = vocabulary.split("_", 3);
		if (temp.length < 3) {
			oldVocabulary.setCharacteristic("");
			oldVocabulary.setProperty("");
		} else {
			oldVocabulary.setCharacteristic(temp[1]);
			oldVocabulary.setProperty(temp[2]);
		}

		Map<String, Object> map = new HashMap<>();
		map.put("rule", slangRule);
		map.put("vocabularys", vs);
		map.put("oldVocabulary", oldVocabulary);
		map.put("characterMap", characterMap);
		map.put("propertyMap", AnalysisSentenceConstant.getPropertyMapByType(AnalysisSentenceConstant.PHYSICAL_PROPERTY_LABEL));
		map.put("start", start);

		return new ModelAndView("slang/rule_edit", map);
	}

	private List<SlangVocabulary> getVocabularys(String rule) {
		List<SlangVocabulary> vs = new ArrayList<>();
		String[] arrayTemp1 = rule.split(";");
		for (String temp1 : arrayTemp1) {
			SlangVocabulary v = new SlangVocabulary();
			String[] arrayTemp2 = temp1.split("_", 3);
			v.setVocabulary(arrayTemp2[0]);
			v.setCharacteristic(arrayTemp2[1]);
			v.setProperty(arrayTemp2[2]);
			vs.add(v);
		}
		return vs;
	}

	public ReturnData edit(Long id, String[] vocabularys, String[] characters, String[] propertys, User user,
			String type, String oldCharacter, String oldProperty) {
		// 获取并修改规则
		SlangRule rule = slangRuleDao.retrieveObjectById(id);

		rule.setVocabulary(getVocabularyByParam(vocabularys[0], rule.getVocabulary(), oldCharacter, oldProperty));
		rule.setRule(getRuleByVocabularys(vocabularys, characters, propertys));
		rule.setType(Integer.parseInt(type));
		rule.setModifierId(user.getUserId());
		rule.setModifierName(user.getRealName());
		slangRuleDao.update(rule);

		return ReturnUtil.success();
	}

	private String getVocabularyByParam(String vocabularyStr, String vocabulary, String oldCharacter,
			String oldProperty) {

		if (!StringUtils.isNullOrEmpty(oldCharacter.trim()) || !StringUtils.isNullOrEmpty(oldProperty.trim())) {
			vocabulary = vocabularyStr + "_" + oldCharacter + "_" + oldProperty;
		} else if (!StringUtils.isNullOrEmpty(oldCharacter) && vocabulary.indexOf("_") > -1) {
			vocabulary = vocabularyStr;
		}
		return vocabulary;
	}

	private String getRuleByVocabularys(String[] vocabularys, String[] characters, String[] propertys) {
		StringBuilder builder = new StringBuilder();
		for (int index = 0; index < propertys.length; index++) {
			String propertie = propertys[index];
			String vocabulary = vocabularys[index].trim();
			String character = characters[index];

			builder.append(vocabulary).append("_").append(character).append("_").append(propertie).append(";");
		}
		return builder.toString();
	}

	public ModelAndView delete(Integer start, Long id) {
		SlangRule slangRule = new SlangRule();
		slangRule.setId(id);
		slangRuleDao.delete(slangRule);
		return index(start);
	}
}
